<div class="bjui-pageHeader">
	<div class="glyphicon glyphicon-th-large">数据库管理</div>
	<a id="export" class="btn btn-default" href="javascript:;" autocomplete="off" data-icon="columns">立即备份</a>
	<a id="optimize" href="{:U('optimize')}" class="btn btn-default" data-icon="gavel">优化表</a>
	<a id="repair" class="btn btn-default" data-icon="undo" href="{:U('repair')}">修复表</a>
	<button type="button" id="restore" class="btn-default" data-icon="exchange" data-url="{:U('Database/index',array('type'=>'import'))}" data-toggle="navtab" data-id="Database-restore">还原管理</button>
</div>
<div class="bjui-pageContent">
	<form action="{:U('export')}" method="post" id="export-form" name="form_do" data-toggle="ajaxform">
		<table class="table table-bordered table-hover table-striped table-top">
			<thead>
				<tr>
					<th>
						<input type="checkbox" class="checkboxCtrl" data-group="tables[]" data-toggle="icheck">
					</th>
					<th>表名</th>
					<th>数据量</th>
					<th>数据大小</th>
					<th>创建时间</th>
					<th>备份状态</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<foreach name='list' item='table'>
					<tr>
						<td>
							<input type="checkbox" name="tables[]" value="{$table.name}" data-toggle="icheck">
						</td>
						<td>{$table.name}</td>
						<td>{$table.rows}</td>
						<td>{$table.data_length|format_bytes}</td>
						<td>{$table.create_time}</td>
						<td class="info">未备份</td>
						<td class="action">
							<a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}" data-toggle="doajax" data-type="get">优化表</a>&nbsp;
							<a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}" data-toggle="doajax" data-type="get">修复表</a>
						</td>

						<!--
                <td>
                <a href="{:U('Database/optimize',array('tablename' => $v['Name']))}" data-toggle="doajax" data-confirm-msg="确定要优化吗？" data-type="get">优化</a>
                <a href="{:U('Database/repair',array('tablename' => $v['Name']))}" data-toggle="doajax" data-confirm-msg="确定要修复吗？" data-type="get">修复</a>       
                </td>  -->
					</tr>
				</foreach>
			</tbody>
		</table>

	</form>

	<script type="text/javascript">
		(function($){
		        var $form = $("#export-form"), $export = $("#export"), tables
		            $optimize = $("#optimize"), $repair = $("#repair");
		
		        $optimize.add($repair).click(function(){
		            $.post(this.href, $form.serialize(), function(data){
		                if(data.status){
		                	$("#export").alertmsg('correct', data.info);
		                } else {
		                    $("#export").alertmsg('error', data.info);
		                }
		                setTimeout(function(){
			                $('#top-alert').find('button').click();
			                //$(that).removeClass('disabled').prop('disabled',false);
			            },1500);
		            }, "json");
		            return false;
		        });
		
		        $export.click(function(){
		            $export.parent().children().addClass("disabled");
		            $export.html("正在发送备份请求...");
		            $.post(
		                $form.attr("action"),
		                $form.serialize(),
		                function(data){
		                    if(data.status){
		                        tables = data.tables;
		                        $export.html(data.info + "开始备份，请不要关闭本页面！");
		                        backup(data.tab);
		                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
		                    } else {
		                        //updateAlert(data.info,'alert-error');
		                        $export.alertmsg('error', data.info);
		                        $export.parent().children().removeClass("disabled");
		                        $export.html("立即备份");
		                        setTimeout(function(){
		        	                $('#top-alert').find('button').click();
		        	                $(that).removeClass('disabled').prop('disabled',false);
		        	            },1500);
		                    }
		                },
		                "json"
		            );
		            return false;
		        });
		
		        function backup(tab, status){
		            status && showmsg(tab.id, "开始备份...(0%)");
		            $.get($form.attr("action"), tab, function(data){
		                if(data.status){
		                    showmsg(tab.id, data.info);
		
		                    if(!$.isPlainObject(data.tab)){
		                        $export.parent().children().removeClass("disabled");
		                        $export.html("备份完成，点击重新备份");
		                        window.onbeforeunload = function(){ return null }
		                        return;
		                    }
		                    backup(data.tab, tab.id != data.tab.id);
		                } else {
		                    $export.alertmsg('error', data.info);
		                    $export.parent().children().removeClass("disabled");
		                    $export.html("立即备份");
		                    setTimeout(function(){
		    	                $('#top-alert').find('button').click();
		    	                $(that).removeClass('disabled').prop('disabled',false);
		    	            },1500);
		                }
		            }, "json");
		
		        }
		
		        function showmsg(id, msg){
		            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
		        }
		    })(jQuery);
	</script>

</div>
<div class="bjui-pageFooter">
	<ul>
		<li>
			<button type="button" class="btn-close" data-icon="close">关闭</button>
		</li>
	</ul>
</div>